package pref.junx.flybird.base.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import butterknife.ButterKnife;
import pref.junx.flybird.R;
import pref.junx.flybird.app.ActivityManager;
import pref.junx.flybird.config.ErrorCodeHelper;
import pref.junx.flybird.network.OnFailedListener;

/**
 * Created by JunX on 17/5/12.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, OnFailedListener {

    protected static final String TAG = BaseActivity.class.getSimpleName();

    // 网络请求结果处理
    protected Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ErrorCodeHelper.REQUEST_SUCCESS:
                    requestSuccessHandler();
                    break;
                case ErrorCodeHelper.REQUEST_FAILED:
                    Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case ErrorCodeHelper.REQUEST_ERROR:
                    Toast.makeText(getApplicationContext(), R.string.network_request_error, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityManager.getAppManager().addActivity(this);
        setContentView(getContentView());
        ButterKnife.bind(this);
        initWindow();
        initWidget();
        initListener();
        initView();
        initData();
    }

    protected abstract int getContentView();

    protected void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.e(TAG, "请求出错");
        Log.e(TAG, e.getLocalizedMessage());
        Log.e(TAG, e.getMessage());
        handler.sendEmptyMessage(-1);
    }

    @Override
    public void onFailed(int errorCode) {
        Log.e(TAG, "返回错误码: " + errorCode);
        Message message = new Message();
        message.what = 1;
        message.obj = errorCode;
        handler.sendMessage(message);
    }

    @Override
    public void onClick(View view) {
        processClick(view);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getAppManager().finishActivity();
    }

    protected void initWidget() {

    }

    protected void initListener() {

    }

    protected void initView() {

    }

    protected void initData() {

    }

    protected void processClick(View view) {

    }

    protected void requestSuccessHandler() {

    }

}
